package com.millioncool.selfpagingsimple.paging;

import android.util.ArrayMap;

import java.util.List;
import java.util.Map;

/**
 * @Author: QDL
 * @CreateDate: 2021/4/26 13:48
 */
public abstract class PagingSource<K, T> {
    PagingPager<T> pagingPager;
    LoadParams<K> currentLoadParams;
    ResultCallBack<K, T> resultCallBack;
    Map<Integer, LoadParams> paramsMap = new ArrayMap<>();
    int pageIndex = 1;

    public PagingSource(LoadParams<K> loadParams) {
        pagingPager = new PagingPager<>(new RequestNetListener() {
            @Override
            public void loadData() {
                if (paramsMap.containsKey(currentLoadParams)) {
                    return;
                }
                PagingSource.this.loadData(loadParams, resultCallBack);
            }

            @Override
            public void refresh() {
                paramsMap = new ArrayMap<>();
                PagingSource.this.loadData(loadParams, resultCallBack);
            }

            @Override
            public void retry() {
                PagingSource.this.loadData(loadParams, resultCallBack);
            }
        });

        this.currentLoadParams = loadParams;
        resultCallBack = new ResultCallBack<K, T>() {
            @Override
            public void onSuccess(LoadParams<K> loadParams, List<T> data) {
                currentLoadParams = loadParams;
                if (paramsMap.isEmpty()) {
                    pagingPager.setRefresh(true);
                    pageIndex = 1;
                } else {
                    pagingPager.setRefresh(false);
                }
                paramsMap.put(pageIndex, loadParams);
                pagingPager.getPagingLiveData().setValue(data);
                pageIndex++;
            }

            @Override
            public void onError(Exception exception) {

            }
        };
        loadData(currentLoadParams, resultCallBack);
    }

    /**
     * 加载数据
     *
     * @param loadParams
     * @param resultCallBack
     * @return
     */
    public abstract void loadData(LoadParams<K> loadParams, ResultCallBack<K, T> resultCallBack);

    public PagingPager<T> getPagingPager() {
        return pagingPager;
    }

    public void setPagingPager(PagingPager<T> pagingPager) {
        this.pagingPager = pagingPager;
    }

    public static class LoadParams<K> {
        K key;

        public LoadParams(K key) {
            this.key = key;
        }

        public K getKey() {
            return key;
        }

        public LoadParams<K> setKey(K key) {
            this.key = key;
            return this;
        }
    }

    public interface ResultCallBack<K, T> {
        /**
         * 成功
         *
         * @param loadParams 下一页参数
         * @param data
         */
        public void onSuccess(LoadParams<K> loadParams, List<T> data);

        /**
         * 失败
         *
         * @param exception
         */
        public void onError(Exception exception);
    }


    public interface RequestNetListener {
        public void loadData();

        public void refresh();

        public void retry();
    }
}
